package lee.vioson.ebook.ui.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Icon
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import kotlinx.coroutines.launch
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.viewmodels.MainViewModel

/**
 *Author:viosonlee
 *Date:2023/6/2
 *DESCRIPTION:
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MainScreen(
    navHostController: NavHostController,
    appContainer: AppContainer,
    viewModel: MainViewModel = viewModel()
) {
    val scope = rememberCoroutineScope()
    Column(
        Modifier
            .fillMaxSize()
            .background(AppTheme.colors.background)
    ) {
        val pageState = rememberPagerState(
            initialPage = 0,
            initialPageOffsetFraction = 0f,
        )
        HorizontalPager(
            4,
            state = pageState,
            verticalAlignment = Alignment.CenterVertically,
            userScrollEnabled = false,
            modifier = Modifier.weight(1f)
        ) { position ->
            when (position) {
                0 -> HomeScreen(navHostController, appContainer)
                1 -> FavoritesScreen(navHostController, appContainer)
                2 -> RecentlyScreen(navHostController, appContainer)
                3 -> MyScreen(navHostController, appContainer)
            }
        }
        TabRow(
            selectedTabIndex = pageState.currentPage,
            modifier = Modifier
                .fillMaxWidth()
                .navigationBarsPadding(),
            indicator = {},
            divider = {}
        ) {
            viewModel.tabs.forEachIndexed { index, tabData ->
                val selected = index == pageState.currentPage
                Tab(selected = selected,
                    selectedContentColor = AppTheme.colors.primary,
                    unselectedContentColor = AppTheme.appColors.tabUnselectColor,
                    onClick = {
                        scope.launch {
                            pageState.scrollToPage(index)
                        }
                    }) {
                    Column(
                        Modifier
                            .fillMaxSize()
                            .padding(vertical = 8.dp),
                        horizontalAlignment = Alignment.CenterHorizontally,
                        verticalArrangement = Arrangement.Center
                    ) {
                        Icon(
                            painter = painterResource(tabData.icon),
                            contentDescription = stringResource(id = tabData.txt),
                            modifier = Modifier.size(30.dp)
                        )
                        Text(text = stringResource(id = tabData.txt), fontSize = 12.sp)
                    }
                }
            }
        }
    }
}